package com.harmontronics.hanma.paas.paas.domain.iam.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmAction;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.valueobject.HmRolePermActionFields;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.persistence.HmRolePermActionRepository;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmRolePermAction;
import java.util.ArrayList;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.harmontronics.hanma.paas.paas.common.PageBase;

import com.harmontronics.hanma.paas.paas.domain.iam.repository.mapper.HmRolePermActionMapper;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.po.HmRolePermActionPO;
import java.util.List;

@Service("hmRolePermActionService")
@DS("#header.App-Unique")
public class HmRolePermActionService{

    @Autowired
    private HmRolePermActionRepository hmRolePermActionRepository;

    @Autowired
    private HmRolePermActionMapper hmRolePermActionMapper;


    public HmRolePermAction findById( Integer id){
            HmRolePermActionPO po =   hmRolePermActionRepository.getById(id);
        return BeanUtil.toBean(po,HmRolePermAction.class);
    }

    public void save(HmRolePermAction hmRolePermAction){
            HmRolePermActionPO po =  BeanUtil.toBean(hmRolePermAction,HmRolePermActionPO.class);
            hmRolePermActionRepository.save(po);
    }

    public void update(HmRolePermAction hmRolePermAction){
            HmRolePermActionPO po =  BeanUtil.toBean(hmRolePermAction,HmRolePermActionPO.class);
            hmRolePermActionRepository.updateById(po);
    }

    public void remove(List<Integer> ids){
            hmRolePermActionRepository.removeByIds(ids);
    }

    public List<HmRolePermAction> getList(){
        List<HmRolePermActionPO> pos =  hmRolePermActionRepository.list();
        return Convert.toList(HmRolePermAction.class,pos) ;
    }

    public PageBase<HmRolePermAction> getList(int pageNo,int pageSize){
        Page<HmRolePermActionPO> page = new Page<>(pageNo, pageSize);
        IPage<HmRolePermActionPO> resultPage =
                    hmRolePermActionRepository.selectPage(Wrappers.emptyWrapper(),page);
        return PageBase.from(resultPage,HmRolePermAction.class);
    }

    public void parseActions(List<HmAction> actions, Long rolePermId) {
        if (CollectionUtils.isNotEmpty(actions)) {
            List<HmRolePermActionPO> rolePermActions = new ArrayList<>();
            for (HmAction action : actions) {
                HmRolePermActionPO rolePermAction =
                        new HmRolePermActionPO().setRolePermId(rolePermId).setActionId(action.getId()).setAccess(action.getAccess());
                rolePermActions.add(rolePermAction);
            }
            hmRolePermActionRepository.saveBatch(rolePermActions);
        }
    }

    public void deleteWithRolePermIds(List<Long> rolePermIds) {
        if (CollectionUtils.isNotEmpty(rolePermIds)) {
            hmRolePermActionMapper.delete(
                    new UpdateWrapper<HmRolePermActionPO>()
                            .in(HmRolePermActionFields.ROLE_PERM_ID, rolePermIds));
        }
    }

}